#include "WProgram.h"
void setup();
void loop();
char sendMsgTo(char getAT);
char returnedOK();
char inByte;
char getData[10];
char getData2[10];
int start=0;
int start2=0;
char inChar;
char inChar2;
int getChair1=0;
int getChair2=0;
int getChair3=0;
int getChairColor1=0;
int getChairColor2=0;
int getChairColor3=0;
void setup(){ 
  Serial.begin(9600);
} 
 
void loop(){ 
 digitalWrite(13,1);
  if(Serial.available() > 0){
   inByte = Serial.read();

   if(start==1){
    for(int i=0;i<13;i++){  
    inChar = Serial.read();  
     getData[i] = inChar;
     if(i == 12){
       for(int j=0;j<=i;j++){
         Serial.print(getData[j]);
         
         if(getData[4] == '3'){
           if(getData[6] == 'Z'){
             //\u7b2c\u4e09\u5f35\u6905\u5b50\u6b63\u88ab\u5ea7\u8457
                 getChair3 = 1;
                   //Serial.println("check1-OK");
                   if(getData[12] == 'd'){
                     getChairColor3 = 2;//\u6548\u679c2
                      Serial.println("check2-OK");
                   }
              }
           }
         }
         
       }
     }
    }
    
    /* if(start2==1){
    for(int i=0;i<6;i++){  
    inChar2 = Serial.read();  
     getData2[i] = inChar2;
     if(i ==5 ){
       for(int j=0;j<=i;j++){
         Serial.print(getData2[j]);
         
         if(getData2[3] == '3'){
           if(getData2[5] == 'd'){
             //\u7b2c\u4e09\u5f35\u6905\u5b50\u6b63\u5728\u8dd1d
                 getChairColor3 = 2;//\u6548\u679c2
                 Serial.println("check2-OK");
                 
              }
           }
         }
         
       }
     }
    }*/
  
   if(inByte == 'C'){
      start = 1; 
   }
  
 /* if(inByte == 'o'){ 
      start2 = 1;
 } */
 
}

if( getChair3 == 1 &&  getChairColor3 == 2 ){
  sendMsgTo('iChair3');  
  Serial.print("c");//\u66ab\u6642
}


}
char sendMsgTo(char getAT){
  if(getAT == 'iChair3'){
   Serial.print("X");
   delay(1100);
   Serial.print("+++");
   delay(1100); 
      if(returnedOK() == 'T'){
         Serial.print("ATDN ICHAIR3,CN");
         delay(1100);
         Serial.print("Connected iChair3..");
        delay(250);
        return 'OK';
      }  
  }   
}

 char returnedOK() {
      char incomingChar[3]; 
      char okString[] = "OK";
      char result = 'n';
      int startTime = millis();
        while (millis() - startTime < 2000 && result == 'n') {  
          if (Serial.available() > 1) {
          //\u7576\u6709\u8cc7\u6599\u9032\u5165\u4e4b\u5f8c\u4f5c\u5224\u65b7
            for (int i=0; i<3; i++) {
              incomingChar[i] = Serial.read();
            }
          if ( strstr(incomingChar, okString) != NULL ) {   //\u6aa2\u67e5\u662f\u5426\u662fOK\u5b57\u5143
            if (incomingChar[0] == 'O' && incomingChar[1] =='K') {
              result = 'T';     //\u5982\u679c\u662f\u7684\u8a71\u5c31\u56de\u50b3T
            }else{
              result = 'F';     //\u5426\u5247\u56de\u50b3F
            }
           }
          }
      return result;
      }
    }

int main(void)
{
	init();

	setup();
    
	for (;;)
		loop();
        
	return 0;
}

